package com.lzxuni.modules.cropinformation.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.annotation.SysLog;
import com.lzxuni.common.constant.LogConstant;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.cropinformation.entity.TblCropInformation;
import com.lzxuni.modules.cropinformation.service.TblCropInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * <p>
 * 作物信息 前端控制器
 * </p>
 *
 * @author rdf
 * @since 2020-11-24
 */
@RestController
@RequestMapping("/LR_TblCropInformation/CropInformation")
public class TblCropInformationController extends BaseController {

    @Autowired
    private TblCropInformationService tblCropInformationService;

    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView("/LR_TblCropInformation/cropinformation/index");
        return mv;
    }

    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination,String queryJson,String cropName,String cropModel){
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        TblCropInformation cropInformation = new TblCropInformation();
        if(StringUtils.isNotEmpty(cropName)){
            cropInformation.setCropName(cropName);
        }
        if(StringUtils.isNotEmpty(cropModel)){
            cropInformation.setCropModel(cropModel);
        }
        PageData pageData = getPageData(tblCropInformationService.queryPage(pageParameter, cropInformation));
        return R.ok().put("data", pageData);
    }

    //新增跳页
    @RequestMapping("/Form")
    public ModelAndView Forma(String keyValue,String type) throws Exception {
        ModelAndView mv = new ModelAndView("/LR_TblCropInformation/cropinformation/form");
        if(keyValue != null){
            type = "update";
            mv.addObject("type", type);
        }else{
            type = "add";
            mv.addObject("type", type);
        }
        return mv;
    }

    //保存
    @SysLog(categoryId = LogConstant.OPERATEID, module = "作物信息", operateType = LogConstant.INSERT)
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue,TblCropInformation tblCropInformation) {
        if(StringUtils.isNotEmpty(keyValue)){
            tblCropInformation.setCropId(keyValue);
            tblCropInformation.setCropName(tblCropInformation.getCropName());
            tblCropInformation.setCropModel(tblCropInformation.getCropModel());
            tblCropInformation.setCropIntroduction(tblCropInformation.getCropIntroduction());
            tblCropInformationService.updateById(tblCropInformation);
            return R.ok().put("data", "修改成功！");
        }else {
            String coprId = UuidUtil.get32UUID();
            tblCropInformation.setCropId(coprId);
            tblCropInformation.setCropName(tblCropInformation.getCropName());
            tblCropInformation.setCropIntroduction(tblCropInformation.getCropIntroduction());
            tblCropInformation.setCreateTime(new Date());
            tblCropInformationService.save(tblCropInformation);
            tblCropInformation.setCropModel(tblCropInformation.getCropModel());
            return R.ok().put("data", "保存成功！");
        }
    }

    @SysLog(categoryId = LogConstant.OPERATEID,module = "作物信息",operateType = LogConstant.DELETE)
    @RequestMapping("/DeleteForm")
    public Object delete(String cropId) {
        tblCropInformationService.removeById(cropId);
        return R.ok("删除成功");
    }
    //新增时查询本年度录入用户信息是否重复
    @RequestMapping("QueryAdd")
    public int QueryRepetition(TblCropInformation tblCropInformation) throws Exception {
//        int cds = tblCropInformationService.queryfarmerYearByCustomId(tblCropInformation);
        int cds = tblCropInformationService.count(new QueryWrapper<TblCropInformation>().eq("crop_name",tblCropInformation.getCropName()).eq("crop_model",tblCropInformation.getCropModel()));
        return cds;
    }
    //修改时查询本年度录入用户信息是否重复
    @RequestMapping("QueryUpdate")
    public int QueryUpdate(TblCropInformation tblCropInformation,String keyValue) throws Exception {
        int cds = tblCropInformationService.queryfarmerUpdateByCropId(tblCropInformation);
//        int cds = tblCropInformationService.count(new QueryWrapper<TblCropInformation>()
//                .eq("crop_name",tblCropInformation.getCropName())
//                .eq("crop_model",tblCropInformation.getCropModel()).eq("crop_id",keyValue));
        return cds;
    }

}

